package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.IssueEntity;
import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.service.IssueService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 计算任务白名单管理
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/issue")
public class IssueController2 {

    @Autowired
    private IssueService issueService;

    @Data
    public static class Param {
        public Long id;
        public String ids;
        private String phoneNumber;
    }

    @RestGrpcMapping("/listIssue.do")
    public NTSTATUS listIssue(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            List<IssueEntity> items = issueService.listIssue();
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getIssue.do")
    public NTSTATUS getIssue(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            IssueEntity item = issueService.getIssue(aa.id);
            result.setData(item);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    @RestGrpcMapping("/setIssue.do")
    public NTSTATUS setIssue(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            IssueEntity aa = params.driverObject(IssueEntity.class);
            Object res = issueService.setIssue(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/deleteIssue.do")
    public NTSTATUS deleteIssue(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            Boolean ret = issueService.deleteIssue(aa.ids);
            result.setData(ret);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
}
